<?php

namespace app\modules\v1\controllers;

use Yii;
use yii\rest\ActiveController;
use yii\web\NotFoundHttpException;
use app\modules\v1\models\Log;

/**
 * json通用遵守
 * {
 *      "id":"",            (文件ID)
 *      "telephone":"",     (手机号码)
 *      "headico":"",       (头像)
 *      "idcard":"",        (身份证)
 *      "img":"",           (图片base64)
 *      "ext":"",           (图片格式)
 * }
 * 
{
    "headico": "true",
    "idcard": "",
    "ext":"png",
    "img":"[base64]",
    "token":"MjAxNy0wMy0wNw==8a72f5a9e00f51bc11a53c5aa871c7c6"
}
 */
class UserFileController extends ActiveController
{
    public $modelClass = 'app\modules\v1\models\Userfile';
    
    public function actions(){
        
        $actions = parent::actions();
        
        unset($actions['index'],$actions['create'],$actions['delete'],$actions['view'],$actions['update']);
        
    }
    
    public function actionIndex($telephone){
        
        $this->log('GET',json_encode(Yii::$app->request->get(),JSON_UNESCAPED_UNICODE));
        
        $modelClass = $this->modelClass;
        
        $model = $modelClass::find()->where(['telephone' => $telephone])->one();
        
        return $model;
        
    }
    
    public function actionCreate(){
        
//        $this->log('POST', Yii::$app->request->getRawBody());
        /*
        $model = new $this->modelClass;
        
        $data = json_decode(Yii::$app->request->getRawBody(),true);
        
        $this->tokenValidate($data['token']);

        $name = md5(microtime()).'.'.$data['ext'];
        
        $img = base64_decode($data['img']);
        
        $data['img'] = 'data';
        
        $this->log('POST', json_encode($data));
        
        $model->telephone = $data['telephone'];
        
        if($data['headico'] == 'true'){
            $model->headico = 'upload/headico/'.$name;
            $res = file_put_contents(Yii::$app->basePath.'/web/upload/headico/'.$name , $img);
        }else if($data['idcard'] == 'true'){
            $model->idcard = 'upload/idcard/'.$name;
            $res = file_put_contents(Yii::$app->basePath.'/web/upload/idcard/'.$name , $img);
        }else{
            $response = Yii::$app->response;
            $response->format = \yii\web\Response::FORMAT_JSON;
            return $response->data = ['headico' => 401, 'idcard' => 401];
        }
        
        if($res == 0){
            return new NotFoundHttpException();
        }
        
        if(!$model->save()){
            return $model->getFirstErrors();
        }
        
        return $model;*/
    }
    
    public function actionUpdate($id){
        
//        $this->log('PUT', Yii::$app->request->getRawBody());

        $model = $this->findModel($id);
        
        $data = json_decode(Yii::$app->request->getRawBody(),true);

        $this->tokenValidate($data['token']);
        
        $img = base64_decode($data['img']);
        
        $data['img'] = 'data';
        
        $this->log('PUT', json_encode($data));
        
        $name = md5(microtime()).'.'.$data['ext'];
        
        if($data['headico'] == 'true'){
            $model->headico = 'upload/headico/'.$name;
            $res = file_put_contents(Yii::$app->basePath.'/web/upload/headico/'.$name , $img);
        }else if($data['idcard'] == 'true'){
            $model->idcard = 'upload/idcard/'.$name;
            $res = file_put_contents(Yii::$app->basePath.'/web/upload/idcard/'.$name , $img);
        }else{
            $response = Yii::$app->response;
            $response->format = \yii\web\Response::FORMAT_JSON;
            return $response->data = ['headico' => 401, 'idcard' => 401];
        }   

        if($res == 0){
            return new NotFoundHttpException();
        }
        
        if(!$model->save()){
            return $model->getFirstErrors();
        }
        
        return $model;
        
    }
    
    public function actionDelete($id){
        /*
        $this->log('DELETE', json_encode(['ID' => $id]));
        
        $model = $this->findModel($id);
        
        $model->delete();
        */
    }
    
    public function actionView($id){
        
        $this->log('GET', json_encode(['ID' => $id]));
        
        $model = $this->findModel($id);

        return $model;
        
    }
    
    protected function findModel($id){
        
        $modelClass = $this->modelClass;
        
        $model = $modelClass::findOne($id);
        
        if($model != null){
            return $model;
        }else{
            throw new NotFoundHttpException('404');
        }
        
    }
    
    protected function log($op,$data){
        
        $log = new Log();
        
        $log->attributes = ['operation' => $op,'class' => 'UserFile','requestData' => $data,'dateTime' => date('Y-m-d H:i:s'),'ip' => Yii::$app->request->userIP];
        
        if(!$log->save()){
            return $log->getFirstError();
        }
        
    }

    public function tokenValidate($token){
        if($token != Yii::$app->params['token']){
            throw new NotFoundHttpException;
        }else{
            return true;
        }
    }
    
}
